################
# Paths and Flags
################
TARGET_PATH = ../bin
TARGET_NAME_OPT = gpeerreview
TARGET_NAME_DBG = $(TARGET_NAME_OPT)dbg
OBJ_PATH = ../obj/$(TARGET_NAME_OPT)
UNAME = $(shell uname -s)
ifeq ($(UNAME),Darwin)
CFLAGS = -I/usr/local/include/SDL -D_THREAD_SAFE
DBG_CFLAGS = $(CFLAGS) -g -D_DEBUG -DDARWIN -DBYTE_ORDER_BIG_ENDIAN -no-cpp-precomp
OPT_CFLAGS = $(CFLAGS) -O3 -DDARWIN -DBYTE_ORDER_BIG_ENDIAN -no-cpp-precomp
DBG_LFLAGS = $(DBG_LIBRARIES) -framework AppKit
OPT_LFLAGS = $(OPT_LIBRARIES) -framework AppKit
else
CFLAGS = -Wall
DBG_CFLAGS = $(CFLAGS) -g -D_DEBUG
OPT_CFLAGS = $(CFLAGS) -O3
DBG_LFLAGS = -lpthread
OPT_LFLAGS = -lpthread
endif

################
# Source
################

CPP_FILES =\
		app.cpp\
		GHashTable.cpp\
		GHtml.cpp\
		GHttp.cpp\
		GQueue.cpp\
		GSocket.cpp\
		GSpinLock.cpp\
		main.cpp\
		sha2.cpp\

HEADER_FILES =\
		app.h\
		GHashTable.h\
		GHtml.h\
		GHttp.h\
		GQueue.h\
		GSocket.h\
		GSpinLock.h\
		sha2.h\
		uitypes.h\

################
# Lists
################

TEMP_LIST_1 = $(CPP_FILES:%=$(OBJ_PATH)/%)
OBJECTS_OPT = $(TEMP_LIST_1:%.cpp=%.o)
OBJECTS_DBG = $(TEMP_LIST_1:%.cpp=%.dbg.o)

################
# Rules
################

dbg : skipsomelines makeobjfolder $(TARGET_PATH)/$(TARGET_NAME_DBG)

opt : skipsomelines makeobjfolder $(TARGET_PATH)/$(TARGET_NAME_OPT)

skipsomelines :
	echo
	echo
	echo
	echo
	echo
	echo
	echo
	echo
	echo

usage:
	#
	# Usage:
	#  make usage   (to see this info)
	#  make clean   (to delete all the .o files)
	#  make dbg     (to build a debug version)
	#  make opt     (to build an optimized version)
	#

makeobjfolder:
	if [ -d $(OBJ_PATH) ] ; then true ; else mkdir -p $(OBJ_PATH) ; fi
	if [ -d $(TARGET_PATH) ] ; then true ; else mkdir -p $(TARGET_PATH) ; fi

$(TARGET_PATH)/$(TARGET_NAME_OPT) : partialcleanopt $(OBJECTS_OPT)
	g++ -O3 -o $(TARGET_PATH)/$(TARGET_NAME_OPT) $(OBJECTS_OPT) $(OPT_LFLAGS)

$(TARGET_PATH)/$(TARGET_NAME_DBG) : partialcleandbg $(OBJECTS_DBG)
	g++ -g -D_DEBUG -o $(TARGET_PATH)/$(TARGET_NAME_DBG) $(OBJECTS_DBG) $(DBG_LFLAGS)

$(OBJECTS_OPT) : $(OBJ_PATH)/%.o : %.cpp $(HEADER_FILES)
	g++ $(OPT_CFLAGS) -c $< -o $@

$(OBJECTS_DBG) : $(OBJ_PATH)/%.dbg.o : %.cpp $(HEADER_FILES)
	g++ $(DBG_CFLAGS) -c $< -o $@

partialcleandbg :
	rm -f $(TARGET_PATH)/$(TARGET_NAME_DBG)

partialcleanopt :
	rm -f $(TARGET_PATH)/$(TARGET_NAME_OPT)

clean : partialcleandbg partialcleanopt
	rm -f $(OBJECTS_OPT)
	rm -f $(OBJECTS_DBG)
